package servlet.user;

import domain.Page;
import domain.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/QueryUserServlet")
public class QueryUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("username");
        System.out.println(name);
        if (name == null) {
            name="";
        }
        String current=req.getParameter("current");
        int curPage=(current==null?1:Integer.parseInt(current));
        UserService us=new UserService();
        //列表来存储数据，直接遍历
        List<User> users=us.findUsersByName(name,curPage);
        req.setAttribute("users", users);
        //获取所有页码的列表
        List<Page> pages=us.getPages(name);
        req.setAttribute("pages", pages);
        req.setAttribute("current",curPage);
        req.setAttribute("total", pages.size());

        req.setAttribute(name, name);
        //跳转
        req.getRequestDispatcher("User_management.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
